package cn.itcast.controller;

import cn.itcast.domain.Account;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Created by leiylong on 2020/4/12
 */

@Controller
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/new")
    public List<Account> findAll() {

        return accountService.findAll();
    }

    /*
    查询所有
     */

    @RequestMapping(value = "/new1")
    @ResponseBody
    public List<Account> And() {
        List<Account> list = accountService.findAll();
        return list;
    }

    /*
    重定向
     */

    @RequestMapping(value = "/new2")
    public String ajax() {
        return "forward:/WEB-INF/pages/new.jsp";
    }

    /*
    根据id查询
     */
    @RequestMapping(value = "/new3")
    @ResponseBody
    public Account findById(Integer id) {
        Account account = accountService.findById(id);
        int i = account.getId();
        String name = account.getName();
        double money = account.getMoney();
        ArrayList<Object> list = new ArrayList<Object>();
        list.add(i);
        list.add(name);
        list.add(money);
        System.out.println(list);
        return account;
    }

    //分页
    @GetMapping("/new4")
    @ResponseBody
    public PageResult<Account> findPage(int page, int size) {
        return accountService.findPage(page, size);
    }

    //添加
    @PostMapping("/new5")
    @ResponseBody
    public Result add(Account account) {
        accountService.add(account);
        return new Result();
    }

    /*
    物理删除
     */
    @PostMapping("/new6")
    @ResponseBody
    public Result delete(Integer id) {
        accountService.delete(id);
        return new Result();
    }

    /*
    逻辑删除
     */
    @PostMapping("/new7")
    @ResponseBody
    public Result updateAndDelete(Integer id) {
        accountService.updateAndDelete(id);
        return new Result();
    }

    //更新数据
    @PostMapping("/new8")
    @ResponseBody
    public Result update(Account account) {
        accountService.update(account);
        return new Result();
    }

}
